<?php
// +----------------------------------------------------------------------
// |店铺公告
// +----------------------------------------------------------------------
// |Data:2016-06-27 下午8:21
// +----------------------------------------------------------------------
// |Author: 吕志雄 <870923001@qq.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;
use Common\Controller\AdminCommonController;

class NoticeController extends AdminCommonController{
    private $_model;
    protected function _initialize(){
        parent::_initialize();
        $this->_model = D('Admin/Notice');
    }

    public function index(){
        $map = array();
        if($_SESSION['administrator']){
            $map['id'] = array('gt',0);
        }else{
            $map['uid'] = $this->uid;
        }
        $list = $this->_model->getList($map,'add_time desc');
        foreach($list['data'] as &$val){
            $detail = D('Common/Shop')->detail($val['shop_id']);
            $val['shopTitle'] = $detail['title'];
        }
        $this->assign($list);
        $this->display();
    }

    public function edit(){
        $id = I('get.id');
        if($id){
            $detail = $this->_model->detail($id);
            $this->assign('detail',$detail);
        }
        $shopList = D('Common/Shop')->getShopList($this->manage_id);
        $this->assign('shopList',$shopList);
        $this->display();
    }

    public function update(){
        if(IS_POST){
            $data = $this->checkPostData();
            empty($data) || !is_array($data) && $this->returnData('请填写表单数据!');
            $result = $this->_model->update($data);
            $msg = $data['id'] ? '编辑' : '添加';
            if($result){
                $this->addHistoryLog($msg.'店铺通告成功');
                $this->returnData($msg.'成功!',1);
            }else{
                $this->returnData($msg.'失败!');
            }
        }else{
            $this->returnData('请求方式错误!');
        }
    }

    public function setStatus(){
        $id = I('get.id');
        empty($id) && $this->returnData('请选择操作项!');
        $detail = $this->_model->detail($id);
        empty($detail) && $this->returnData('数据不存在!');
        $status = $detail['status'] == 1 ? 2 : 1;

        $data = array(
            'status'=>$status,
            'id'=>$id,
            'edit_time'=>time()
        );
        $result = $this->_model->update($data);
        $msg = $status == 1 ? '启用' : '禁用';
        if($result){
            $this->addHistoryLog($msg.'店铺公告成功');
            $this->returnData($msg.'成功!',1);
        }else{
            $this->returnData($msg.'失败!');
        }
    }

    public function del(){
        $id = I('get.id');
        empty($id) && $this->returnData('请选择操作项!');
        $result = $this->_model->del($id);
        if($result){
            $this->addHistoryLog('删除店铺通告成功');
            $this->returnData('删除成功!',1);
        }else{
            $this->returnData('删除失败!');
        }
    }

    /**
     * 检测表单数据
     * @return array
     */
    private function checkPostData(){
        $id = I('post.id');
        $shop_id = I('post.shop_id');
        $desc = I('post.desc');

        empty($shop_id) && $this->returnData('请选择分店!');
        if($_SESSION['administrator']){
            $detail = D('Common/Shop')->detail($shop_id);
            $uid = $detail['uid'];
        }else{
            $uid = $this->manage_id;
        }
        empty($desc) && $this->returnData('请输入店铺公告!');
        if(abslength($desc) > 200) $this->returnData('公告内容长度1~200个字符!');
        $data = array(
            'shop_id'=>$shop_id,
            'desc'=>$desc,
            'uid'=>$uid,
        );
        if($id){
            $data['id'] = $id;
            $data['edit_time'] = time();
        }else{
            $data['add_time'] = time();
        }
        return $data;
    }
}
?>